#include "bsp_ft5426.h"
#include "bsp_gpio.h"
#include "bsp_int.h"
#include "bsp_delay.h"
#include "bsp_i2c.h"
#include "stdio.h"

/* FT5426触摸屏驱动芯片初始化 */
void ft5426_init(void)
{
	/* IO接口初始化 */
	IOMUXC_SetPinMux(IOMUXC_UART5_TX_DATA_I2C2_SCL, 1);				/* 复用为I2C_SCL, 使能 SION 比特位 */
	IOMUXC_SetPinConfig(IOMUXC_UART5_TX_DATA_I2C2_SCL, 0x70B0);		/* IO特性, 使能开漏模式 */

	IOMUXC_SetPinMux(IOMUXC_UART5_RX_DATA_I2C2_SDA, 1);				/* 复用为I2C_SDA */
	IOMUXC_SetPinConfig(IOMUXC_UART5_RX_DATA_I2C2_SDA, 0x70B0);		/* IO特性，使能开漏模式 */


	/* CT_INT接口初始化 */
	IOMUXC_SetPinMux(IOMUXC_GPIO1_IO09_GPIO1_IO09, 0);				/* 复用为GPIO1_IO09 */
	IOMUXC_SetPinConfig(IOMUXC_GPIO1_IO09_GPIO1_IO09, 0xf080);

	gpio_pin_config_t ctint_config;
	ctint_config.direction = kGPIO_DigitalInput;
	ctint_config.interruptMode = kGPIO_IntRisingOrFallingEdge;			/* 双边沿触发 */
	gpio_init(GPIO1, 9, &ctint_config);

	/* 使能中断，注册中断 */
	GIC_EnableIRQ(GPIO1_Combined_0_15_IRQn);						/* 启用GIC IRQ */
	system_register_irqhandler(GPIO1_Combined_0_15_IRQn, gpio1_0_15_irqhandler, NULL);
	gpio_enableint(GPIO1, 9);										/* 启用gpio中断 */


	/* CT_RST接口初始化 */
	IOMUXC_SetPinMux(IOMUXC_SNVS_SNVS_TAMPER9_GPIO5_IO09, 0);		/* 复用为GPIO5_IO09 */
	IOMUXC_SetPinConfig(IOMUXC_SNVS_SNVS_TAMPER9_GPIO5_IO09, 0x10B0);

	gpio_pin_config_t ctrst_config;
	ctrst_config.direction = kGPIO_DigitalOutput;							
	ctrst_config.outputLogic = 1;											
	gpio_init(GPIO5, 9, &ctrst_config);


	/* 复位FT5426芯片 */
	gpio_pinwrite(GPIO5, 9, 0);										/* 复位FT5426触摸芯片 */
	delayms(50);
	gpio_pinwrite(GPIO5, 9, 1);										/* 停止复位FT5426触摸芯片 */
	delayms(50);

	/* I2C接口初始化 */
	i2c_init(I2C2);

	/* FT5426芯片初始化 */
	unsigned char fwLibVersion[2] = {0};
	unsigned short fwVer = fwLibVersion[0] << 8 | fwLibVersion[1];

	unsigned char addr = 0x38;
	for(addr=0; addr<0x80; addr++)
	{
		ft5426_readdata_len(addr, FT5426_IDGLIB_VERSION, 2, fwLibVersion);
	}

	ft5426_readdata_len(addr, FT5426_IDGLIB_VERSION, 2, fwLibVersion);
	printf("touch fireware version addr=%02x fwLibVersion=%02x%02x\r\n", addr, fwLibVersion[0], fwLibVersion[1]);
	

}

void gpio1_0_15_irqhandler(IRQn_Type irq, void *userparam)
{

	if(GPIO1->ISR & (1 << 9)){
		//empty code
	}

	/* 清除中断标志位 */
	gpio_clearintflags(GPIO1, 9);
}


/* ft5426 读一个字节数据, 返回值读取到寄存器的值 */
unsigned char ft5426_readonebyte(unsigned char addr, unsigned char reg)
{
	unsigned char value = 0;

	struct i2c_transfer masterXfer;

	masterXfer.slaveAddress = addr;
	masterXfer.direction = kI2C_Read;
	masterXfer.subaddress = reg;
	masterXfer.subaddressSize = 1;
	masterXfer.data = &value;
	masterXfer.dataSize = 1;

	i2c_master_transfer(I2C2, &masterXfer);


	return value;
}

/* Aft5426 写一个字节数据, 返回值错误类型 */
unsigned char ft5426_writeonebyte(unsigned char addr, unsigned char reg, unsigned char data)
{
	unsigned char ret = 0;

	struct i2c_transfer masterXfer;

	masterXfer.slaveAddress = addr;
	masterXfer.direction = kI2C_Write;
	masterXfer.subaddress = reg;
	masterXfer.subaddressSize = 1;
	masterXfer.data = &data;
	masterXfer.dataSize = 1;

	if(i2c_master_transfer(I2C2, &masterXfer))
	{
		/* 出错 */
		ret = 1;
	}

	return ret;
}

/* ft5426 读取多个寄存器的数据 */
unsigned char ft5426_readdata_len(unsigned char addr, unsigned char reg, unsigned char len, unsigned char *buf)
{
	unsigned char ret = 0;
	struct i2c_transfer masterXfer;

	masterXfer.slaveAddress = addr;
	masterXfer.direction = kI2C_Read;
	masterXfer.subaddress = reg;
	masterXfer.subaddressSize = 1;
	masterXfer.data = buf;
	masterXfer.dataSize = len;

	if(i2c_master_transfer(I2C2, &masterXfer))
	{
		/* 出错 */
		ret = 1;
	}

	return ret;
}

